﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports VisualStudioHelper.vbp
Imports VisualStudioHelper



Public Class ConfrontoVBP

    Private PathOfProject As String
    Private vsh As New VisualStudioHelper.vbp
    'Private pvbp As VisualStudioHelper.vbp.percorsiVbp

    Private vbpDX As New VisualStudioHelper.analisiVBP
    Private vbpSX As New VisualStudioHelper.analisiVBP

#Region "Regex"

    ''' <summary>
    ''' Trasforma una riga che inizia con Module in una riga batch
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetRegQForKeyModule() As String
        'esempio:
        'Module=BtrApi32; ..\..\pervasive\BtrApi32.bas
        'Class=API_Directory; class\API_Directory.cls
        Return "(Module|Class)=.+;\s+(?<percorso>.+)"

    End Function

    Private Function GetRegQForKeyForm() As String
        'esempio:
        'Form=UI\frmFarma.frm
        'UserControl=UI\userControls\myButton.ctl
        Return "(UserControl|Form)=(?<percorso>.+)"
    End Function

#End Region


    ''' <summary>
    ''' in base al PathOfProject valorizzato esternamente, calcola il percorso assoluto di ogni file
    ''' </summary>
    ''' <param name="percorso"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function RelativeToAbsolutePath(percorso As String) As String
        Dim regQuery As String = "\.\.\\"
        Dim relpathCount As Short = 0
        Dim fi As New FileInfo(PathOfProject)
        Dim fld As New DirectoryInfo(PathOfProject)
        Dim ftmp As DirectoryInfo = fld

        Dim re As New Regex(regQuery)
        Dim m As Match = re.Match(percorso)
        Do While m.Success
            relpathCount += 1
            ftmp = ftmp.Parent
            m = m.NextMatch
        Loop

        Return Path.Combine(ftmp.FullName, percorso.Substring(3 * relpathCount))

    End Function

#Region "sfoglia"

    Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
        'apro il file del progetto da elaborare

        Dim ofd As New OpenFileDialog
        ofd.InitialDirectory = "D:\sviluppo\"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = ofd.FileName
        End If

    End Sub

    Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
        'apro il file del progetto da elaborare

        Dim ofd As New OpenFileDialog
        ofd.InitialDirectory = "D:\sviluppo\"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            TextBox2.Text = ofd.FileName
        End If
    End Sub
#End Region

#Region "MAIN Procedura"

    Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click

        Dim fi As New FileInfo(TextBox1.Text)
        PathOfProject = fi.DirectoryName()
        If Not File.Exists(TextBox1.Text) Or Not File.Exists(TextBox2.Text) Then
            Exit Sub
        End If

        My.Settings.lastvbp1 = TextBox1.Text
        My.Settings.lastvbp2 = TextBox2.Text

        'riempie l'array con i vari percorsi
        Dim vbpAL As New VisualStudioHelper.analisiVBP
        vbpAL = readFilePathFromVBP(TextBox1.Text)

        Dim vbpAR As New VisualStudioHelper.analisiVBP
        vbpAR = readFilePathFromVBP(TextBox2.Text)

        'confronta le liste da DX a SX e viceversa

        'confronto lista files
        dgv1F.DataSource = searchDiff(vbpAL.vbpFiles, vbpAR.vbpFiles)
        dgv2F.DataSource = searchDiff(vbpAR.vbpFiles, vbpAL.vbpFiles)

        dgv1R.DataSource = searchDiff(vbpAL.vbpRef, vbpAR.vbpRef)
        dgv2R.DataSource = searchDiff(vbpAR.vbpRef, vbpAL.vbpRef)

        dgv1O.DataSource = searchDiff(vbpAL.vbpObj, vbpAR.vbpObj)
        dgv2O.DataSource = searchDiff(vbpAR.vbpObj, vbpAL.vbpObj)

        'ridimensiona le griglie
        Dim righetotaliSX As Integer = dgv1F.Rows.Count + dgv1O.Rows.Count + dgv1R.Rows.Count
        Dim righetotaliDX As Integer = dgv2F.Rows.Count + dgv2O.Rows.Count + dgv2R.Rows.Count

        '.Height = dgv1F.Rows.Count * 100 / righetotaliSX
        'TableLayoutPanel1.Height = dgv1F.Rows.Count * 100 / righetotaliSX
        'TableLayoutPanel1.Height = dgv1F.Rows.Count * 100 / righetotaliSX

    End Sub

    Private Function searchDiff(ByVal DX As List(Of String), ByVal SX As List(Of String)) As List(Of percorsiVbp)
        Dim result As New List(Of percorsiVbp)

        For Each s As String In DX
            If SX.IndexOf(s) < 0 Then result.Add(New percorsiVbp(s))
        Next

        Return result

    End Function
    Private Function searchDiff(ByVal DX As List(Of Reference), ByVal SX As List(Of Reference)) As List(Of percorsiVbp)
        Dim result As New List(Of percorsiVbp)

        For i As Integer = 0 To DX.Count - 1 'Each s As String In DX
            Dim j As Integer = SX.IndexOfName(DX(i))
            If j < 0 Then
                'questa reference non è presente
                result.Add(New percorsiVbp("Mancante: " & DX(i).nome & " V" & DX(i).versione & "." & DX(i).subversion))
            Else
                'controllo anche se la versine è uguale
                If SX(j).versione = DX(i).versione Then
                    If Not SX(j).subversion = DX(i).subversion Then
                        result.Add(New percorsiVbp("Revisione: " & DX(i).nome & " V" & SX(j).versione & "." & SX(j).subversion & "/V" & DX(i).versione & "." & DX(i).subversion))
                    End If
                Else
                    result.Add(New percorsiVbp("Versione: " & DX(i).nome & " V" & SX(j).versione & "." & SX(j).subversion & "/V" & DX(i).versione & "." & DX(i).subversion))
                End If
            End If
        Next

        Return result

    End Function
    Private Function readFilePathFromVBP(fullfileName As String) As VisualStudioHelper.analisiVBP

        Dim i As Integer = 0    'conta le righe con percorsi da modificare
        Dim j As Integer = 0    'conta le righe totali
        Dim fi As New FileInfo(fullfileName)
        Dim sr As StreamReader = fi.OpenText()

        Dim rowscount As Integer = 100
        Dim rows(100) As String

        'Dim vbp(100) As String
        Dim result As New analisiVBP

        Do Until sr.Peek < 0
            rows(j) = sr.ReadLine

            If CheckBox2.Checked Then
                Dim r As String = RicavaPercorsoFromRow(rows(j), vsh.GetRegQForGenericLine())
                If r <> vbNullString Then
                    If CheckBox1.Checked Then
                        result.vbpObj.Add(r)
                    Else
                        result.vbpObj.Add(r.ToLower)
                    End If
                End If
            Else

                If rows(j).IndexOf("Module") = 0 Or rows(j).IndexOf("Class") = 0 Then
                    Dim r As String = RelativeToAbsolutePath(RicavaPercorsoFromRow(rows(j), vsh.GetRegQForKeyModule()))
                    If r <> vbNullString Then
                        If CheckBox1.Checked Then
                            result.vbpFiles.Add(r)
                        Else
                            result.vbpFiles.Add(r.ToLower)
                        End If
                    End If

                ElseIf rows(j).IndexOf("Form") = 0 Or rows(j).IndexOf("UserControl") = 0 Then
                    Dim r As String = RelativeToAbsolutePath(RicavaPercorsoFromRow(rows(j), vsh.GetRegQForKeyForm()))
                    If r <> vbNullString Then
                        If CheckBox1.Checked Then
                            result.vbpFiles.Add(r)
                        Else
                            result.vbpFiles.Add(r.ToLower)
                        End If
                    End If
                ElseIf rows(j).IndexOf("Object") = 0 Then
                    Dim r As String = (RicavaPercorsoFromRow(rows(j), vsh.GetRegQForKeyObject()))
                    If r <> vbNullString Then

                        If CheckBox1.Checked Then
                            result.vbpObj.Add(r)
                        Else
                            result.vbpObj.Add(r.ToLower)
                        End If
                    End If
                ElseIf rows(j).IndexOf("Reference") = 0 Then

                    Dim r As Reference = RicavaReferenceFromRow(rows(j))



                    If Not r Is Nothing Then
                        If CheckBox1.Checked Then
                            result.vbpRef.Add(r)
                        Else
                            result.vbpRef.Add(r.tolower)
                        End If
                    End If
                End If

            End If

            j += 1
            If j >= rowscount Then
                rowscount = rowscount + 10
                'Array.Resize(vbp, rowscount + 1)
                Array.Resize(rows, rowscount + 1)
            End If
        Loop
        sr.Close()

        Return result


    End Function

#End Region

    ''' <summary>
    ''' Esegue la regex per ottenere il valore del percorso del file da analizzare
    ''' </summary>
    ''' <param name="row"></param>
    ''' <param name="regQuery"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function RicavaPercorsoFromRow(row As String, regQuery As String) As String

        Dim re As New Regex(regQuery)
        If re.IsMatch(row) Then
            Return re.Match(row).Groups("percorso").Value
        End If

        Return vbNullString

    End Function

    Private Function RicavaReferenceFromRow(row As String) As Reference

        Return New Reference(row)

    End Function

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'verifico l'aspetto grafico della form per adattarlo allo stile grafico MDI di menu e dock.fill
        If Me.IsMdiChild Then

            TableLayoutPanel1.Dock = DockStyle.Fill
            MenuStrip1.Visible = False
        Else
            TableLayoutPanel1.Dock = DockStyle.Bottom
            MenuStrip1.Visible = True
        End If

    End Sub

End Class

